<?php
namespace App\Services\Adv\Adv;

use App\Models\Adv\Adv;
use App\Services\Adv\ValidateAdv;
use App\Services\Manage\ManageIndex;
/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   ws
 * @Last Modified time: 2018-06-12 
 */
class AdvLogic
{
    // 添加新的广告位
    public function addAdv($data)
    {
        $rst = Adv::insertGetId($data);

        if ($rst === false) {
            return auto_failure ($data,'广告位添加失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未作改动！');
        }
        return auto_return(['id' => $rst],'广告位添加成功！');
    }

    // 获取某个广告位详情
    public function advInfo($data): array
    {
        $rst = Adv::where('id',$data['id'])->first();
        if ($rst == null) {
            return auto_failure ($data,'广告位不存在！');
        }
        $rst['cate_ids']  = (new AdvClassifyLogic())->getParentIds($rst['c_id']);
      
        return auto_return($rst, '广告详情获取成功！');
    }

    // 某广告位信息编辑修改
    public function advEdit($data)
    {
        $rst = Adv::where('id',$data['id'])->update($data);
        if ($rst === false) {
            return auto_failure ($data,'广告位信息更改失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位信息未作改动！');
        }

        return auto_return(['number' => $rst],'广告位信息更改成功！');
    }

     // 删除某个广告位
    public function advDelete($data): array
    {
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if (intval($manageAppId) != 11) {
            $list = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*', 'adv_classify.name as classify_name')
            ->where([['adv_classify.app_id', '=', intval($manageAppId)],['adv.id', '=', intval($data['id'])],['adv.status', '<>', -1]])
            ->first();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作删除！');
            }
        }
        $status['status'] = -1;
        $status['update_time'] = time();
        $rst = Adv::where('id', $data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位删除失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生删除操作！');
        }

        return auto_return(['number' => $rst], '广告位删除成功！');
    }
     // 启用某个广告位
    public function advEnable($data): array
    {
       //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if (intval($manageAppId) != 11) {
            $list = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*', 'adv_classify.name as classify_name')
            ->where([['adv_classify.app_id', '=', intval($manageAppId)],['adv.id', '=', intval($data['id'])],['adv.status', '<>', -1]])
            ->first();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['status'] = 1;
        $status['update_time'] = time();
        $rst = Adv::where('id', $data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位启用失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位启用成功！');
    }
    // 禁用某个广告位
    public function advDisable($data): array
    {
       //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if (intval($manageAppId) != 11) {
            $list = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*', 'adv_classify.name as classify_name')
            ->where([['adv_classify.app_id', '=', intval($manageAppId)],['adv.id', '=', intval($data['id'])],['adv.status', '<>', -1]])
            ->first();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['status'] = 0;
        $status['update_time'] = time();
        $rst = Adv::where('id', $data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位禁用失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位禁用成功！');
    }
     // 审核通过某个广告位
    public function advAudit($data): array
    {
       //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if (intval($manageAppId) != 11) {
            $list = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*', 'adv_classify.name as classify_name')
            ->where([['adv_classify.app_id', '=', intval($manageAppId)],['adv.id', '=', intval($data['id'])],['adv.status', '<>', -1]])
            ->first();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['audit_status'] = 1;
        $status['auditor_id'] = $data['auditor_id'];
        $status['update_time'] = time();
        $rst = Adv::where('id', $data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位审核通过失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位审核通过成功！');
    }
     // 审核失败/不通过某个广告位
    public function advAuditFailure($data): array
    {
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if (intval($manageAppId) != 11) {
            $list = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*', 'adv_classify.name as classify_name')
            ->where([['adv_classify.app_id', '=', intval($manageAppId)],['adv.id', '=', intval($data['id'])],['adv.status', '<>', -1]])
            ->first();
            if ($list == null) {
                return auto_failure($data, '该广告位分类不属于本应用，无操作权限！');
            }
        }
        $status['audit_status'] = -1;
        $status['auditor_id'] = $data['auditor_id'];
        $status['update_time'] = time();
        $rst = Adv::where('id', $data['id'])
                    ->update($status);
        if ($rst === false) {
            return auto_failure ($data,'广告位操作失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'广告位未发生操作！');
        }

        return auto_return(['number' => $rst], '广告位操作成功！');
    }
     // 获取某个分类广告位 非删除
    public function advClassList($data): array
    {
        $_where = [
            ['adv.status', '=', 1]
        ];
        $data['c_id'] && $_where[] = ['adv.c_id', '=', $data['c_id']];
        $arr = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*')
            ->where($_where)
            ->orderBy('order', 'asc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($arr);
    }
     // 不同状态广告位列表获取
    public function advDifferenceStatus($data): array
    {
         $arr = Adv::where([['status', '=', 1],['audit_status', '=', $data['audit_status']]])
            ->orderBy('id', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($arr);
    }

    // 全部广告位列表获取
    public function advList($data)
    {
            $list = Adv::where('status', '<>', -1)
            ->paginate(10);

        return auto_success($list);
    }
     // 规定上架时间内 正常状态 审核通过的广告位等
    public function advTimeLimit($data): array
    {
        
        if (!empty($data['action'])) {
            $rst  = (new AdvClassifyLogic())::where('adv_classify.action', '=', $data['action'])->first();
            if ($rst == null) {
                return auto_failure($data, '分类标识不存在！');
            }
            $data['c_id'] = $rst['id'];
        }
        $time = time();
        $arr = Adv::where([['c_id', '=', $data['c_id']],['status', '=', 1],['audit_status','=',1],['start_time','<=',$time ],['end_time','>=',$time]])
            ->limit($data['limit'])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();           
        return auto_return($arr);
    }

   
}